﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;
using System.Collections;
using MixScriptWeb.Share;
using MixScript.BLL;


namespace MixScriptWeb.Accounting
{
    public partial class fCOA : cAccCommon, IMIxCommon
    {
        private string errMsg = "";
        private static AccMaster mn;
        private static cCOA oCoa;
        private static GroupCOA oGroupCoa;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                oCoa = new cCOA();
                btnAdd.Attributes.Add("onclick", "openWndChild('PopUpCoaP.aspx','210','450')");
            }
            PopulateTree();
            mn = (AccMaster)this.Master;
            hideStatusBar();
            if (Session["selected"] != null)
                LoadAccount((String)Session["selected"]);
        }
        public bool isvalid()
        { return true; }
        public void PopulateTree()
        {
            //tvCOA.DataSource = null;
            //tvCOA.DataBind();
            DataTable dt = new DataTable();
            Hashtable ht = new Hashtable();
            oGroupCoa = new GroupCOA();
            oGroupCoa.getGroupCOA(ref dt);
            errMsg = "";
            foreach (DataRow r in dt.Rows)
            {
                int nodeLvl = int.Parse(r["idcoa"].ToString());
                string nodeParent = r["Parentid"].ToString();
                string nodeName = nodeLvl.ToString() + "-" + r["coaName"].ToString();
                TreeNode tNode = new TreeNode(nodeName.TrimEnd());
                if (Session["Selected"] != null)
                    if (Convert.ToString(Session["Selected"]).Split('-')[0] == nodeName.TrimEnd().Split('-')[0])
                        tNode.Select();

                ht.Add(nodeLvl.ToString(), tNode);
                if (nodeParent.Trim() == string.Empty)
                {
                    tvCOA.Nodes.Add(tNode);
                }
                else
                {

                    if (ht.ContainsKey(nodeParent))
                    {
                        TreeNode tn = new TreeNode();
                        tn = (TreeNode)ht[nodeParent];
                        tn.ChildNodes.Add(tNode);
                    }
                    else
                    {
                        errMsg += nodeParent + " ";
                    }
                }
            }
            ht.Clear();
        }
        protected void gvdetail_RowDataBound(Object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Panel pnl = new Panel();
                if (e.Row.Cells[3].Text.Trim().Length > 6)
                {

                    pnl = (Panel)e.Row.Cells[0].FindControl("pnlAdd");
                    pnl.Style["visibility"] = "hidden";

                }


            }
        }
        protected void tvCOA_SelectedNodeChanged(object sender, EventArgs e)
        {
            gvDetail.DataSource = null;
            gvDetail.DataBind();
            TreeView tv = (TreeView)sender;
            TreeNode tn = tv.SelectedNode;
            LoadAccount(tn.Text);

        }
        private void LoadAccount(String id)
        {
            try
            {
                Session["Selected"] = id;
                int ix = id.IndexOf('-');
                string pid = id.Substring(0, ix).Trim();

                string judul = id.Split('-')[1].ToString();
                lblJudul.Text = judul;

                btnEdit.Attributes.Clear();
                btnAdd.Attributes.Clear();
                btnAddAccount.Attributes.Clear();
                btnSubGroupAdd.Attributes.Clear();

                btnAddAccount.Visible = false;
                btnSubGroupAdd.Visible = false;
                btnAdd.Visible = false;
                if (pid.Length > 2)
                {
                    btnAddAccount.Visible = true;
                    string sname = id.Substring(ix + 1, id.Length - (ix + 1)).Trim();

                    oCoa = new cCOA();
                    DataTable dt = new DataTable();
                    int iparent = 0;
                    int.TryParse(pid, out iparent);


                    if (oCoa.getSubCoa(ref dt, iparent))
                    {
                        btnEdit.Attributes.Add("onclick", "openWndChild('PopUpCoaP.aspx?ID=" + pid + "&type=E','240','450')");
                        btnSubGroupAdd.Attributes.Add("onclick", "openWndChild('PopUpCoaP.aspx?ID=" + pid + "&type=A','240','450')");
                        btnAddAccount.Attributes.Add("onclick", "openWndChild('PopUpCoa.aspx?PID=" + pid + "&type=A','400','450')");
                        gvDetail.DataSource = dt;
                        gvDetail.DataBind();
                    }
                    else
                    {
                        mn = (AccMaster)this.Master;
                        ShowStatusBar("Record Not Found", msgType.msgError);
                    }

                }
                else
                {
                    btnAddAccount.Visible = false;
                    btnSubGroupAdd.Visible = true;
                    btnAdd.Visible = true;
                    btnEdit.Attributes.Add("onclick", "openWndChild('PopUpCoaP.aspx?PS=P&ID=" + pid + "&type=E','210','450')");
                    btnAdd.Attributes.Add("onclick", "openWndChild('PopUpCoaP.aspx?PS=P&ID=" + pid + "&type=A','210','450')");
                    btnSubGroupAdd.Attributes.Add("onclick", "openWndChild('PopUpCoaP.aspx?PS=S&ID=" + pid + "&type=A','240','450')");
                }
            }
            catch (Exception ex)
            {
                ShowStatusBar(ex.Message, msgType.msgError);
            }
        }

        protected void delete()
        {
            try
            {
                cReferences oReff = new cReferences();
                bool isok = true;
                int i = 0;
                foreach (GridViewRow gr in gvDetail.Rows)
                {

                    if (((CheckBox)gr.Cells[0].FindControl("cbitem")).Checked)
                    {
                        string sid = gr.Cells[1].Text;

                        if (!oReff.deleteRowDB(new string[] { "BANKNUM" }, new string[] { sid }, "MST_BANK", out errMsg))
                        {
                            ShowStatusBar(em.getError(9) + sid + " " + errMsg);
                            isok = false;
                            continue;
                        }
                        else
                        {
                            i++;
                        }
                    }

                }

                if (i == 0 && isok)
                {
                    ShowStatusBar("Please Select Item First");
                }
                else if (isok)
                {
                    ShowStatusBar("Delete Completed");
                }
            }
            catch (Exception ex)
            {
                ShowStatusBar(ex.Message);
            }
        }
        protected void btnDelete_click(object sender, EventArgs e)
        { }
        protected void gvdetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "deleting")
            {
                string code = (string)e.CommandArgument;
                cCOA coa = new cCOA();
                if (coa.DeleteCOA(code.TrimEnd()))
                {
                    ShowStatusBar("Delete Completed");
                }
                else { }

                if (Session["selected"] != null)
                    LoadAccount((String)Session["selected"]);
            }
        }
    }

}